package com.cn.springcloudwechat.serviceImpl;

import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.YmAnswerMapper;
import com.cn.springcloudwechat.mapper.mapper.YmPraiseMapper;
import com.cn.springcloudwechat.mapper.mapper.YmUserCollectionMapper;
import com.cn.springcloudwechat.mapper.mapper.YmUserInfromMapper;
import com.cn.springcloudwechat.model.*;
import com.cn.springcloudwechat.model.articleAndMenu.YmArticle;
import com.cn.springcloudwechat.model.dto.ArticleDetailsDto;
import com.cn.springcloudwechat.model.dto.ArticleDto;
import com.cn.springcloudwechat.model.find.communication.YmAnswerVO;
import com.cn.springcloudwechat.model.find.communication.YmExchange;
import com.cn.springcloudwechat.service.YmUserInfromService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class YmUserInfromServiceImpl implements YmUserInfromService{

    @Autowired
    private YmUserInfromMapper ymUserInfromMapper;

    @Autowired
    private YmUserCollectionMapper ymUserCollectionMapper;

    @Autowired
    private YmAnswerMapper ymAnswerMapper;

    @Autowired
    private  YmPraiseMapper ymPraiseMapper;


    /**
     * 用户系统消息查询
     * @param userId
     * @return
     */
    @Override
    public List<Map<String,Object>> getInformation(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL, "userId"));
        List<Map<String,Object>> list=ymUserInfromMapper.getInformation(userId);
        return list;
    }

    /**
     * 用户服务提醒查询
     * @param userId
     * @return
     */
    @Override
    public List<Map<String,Object>> getRemind(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL, "userId"));
        List<Map<String,Object>> list=ymUserInfromMapper.getRemind(userId);
        return list;
    }

    /**
     * 订单
     * @param userId
     * @param orderPayState
     * @return
     */
    @Override
    public List<Map<String,Object>> getYmOrder(Long userId, String orderPayState) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL, "userId"));
        List<Map<String,Object>> list=ymUserInfromMapper.getYmOrder(userId,orderPayState);

        return list;
    }

    /**
     * 交流圈
     * @param userId
     * @return
     */
    @Override
    public List<Map<String,Object>> getYmExchange(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL, "userId"));
        List<Map<String,Object>> list=ymUserInfromMapper.getYmExchange(userId);
        return list;
    }







    /**
     * 读取状态修改
     * @param answerId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateReadState(Long answerId) {
        Assert.notNull(answerId,String.format(ErrorUtil.PARAMETER_NULL, "answerId"));
        Integer integer=ymUserInfromMapper.updateReadState(answerId);
        return integer;
    }

    @Override
    public Integer updateState(Long informId) {
        Assert.notNull(informId,String.format(ErrorUtil.PARAMETER_NULL, "informId"));
        Integer integer=ymUserInfromMapper.updateState(informId);
        return integer;
    }


    @Override
    public List<ArticleDto> getArticleDto() {
        return ymUserInfromMapper.getArticleDto();
    }

    @Override
    public ArticleDetailsDto getArticleDetailsDto(Long articleId) {
        ArticleDetailsDto articleDetailsDto= ymUserInfromMapper.getArticleDetailsDto(articleId);
        List<String> url = ymUserInfromMapper.getAttachment(articleId);
        List<YmAnswerVO> ymAnswers = ymAnswerMapper.queryYmAnswerDetail(articleId,1);
        YmPraise ymPraise = new YmPraise();
        ymPraise.setPraiseTypeId(articleId);
        ymPraise.setPraiseType("1");
        //todo
        Long userId=ThreadLocalUtil.getUser().getUserId();
        ymPraise.setUserId(userId);
        String praiseType = ymPraise.getPraiseType();
        Long praiseTypeId = ymPraise.getPraiseTypeId();
        Assert.notNull(praiseTypeId,String.format(ErrorUtil.PARAMETER_NULL,"praiseTypeId"));
        Assert.notNull(praiseType,String.format(ErrorUtil.PARAMETER_NULL,"praiseType"));
        int i = ymPraiseMapper.praiseCount(ymPraise);
        articleDetailsDto.setLikeNumber((long) i);
        articleDetailsDto.setAttachment_url(url);
        articleDetailsDto.setYmAnswerVOS(ymAnswers);
        articleDetailsDto.setReplyNumber((long) ymAnswers.size());

        return articleDetailsDto;
    }

    @Override
    public List<Map<String, Object>> getInteractiveMessage(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        List<Map<String, Object>> list=ymUserInfromMapper.getInteractiveMessage(userId);
        return list;
    }

    @Override
    public List<Map<String, Object>> getDoctorAnswerUser(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        List<Map<String, Object>> list=ymUserInfromMapper.getDoctorAnswerUser(userId);
        return list;
    }

    @Override
    public List<Map<String, Object>> getArticleReply(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        List<Map<String, Object>> list=ymUserInfromMapper.getArticleReply(userId);
        return list;
    }

    @Override
    public List<Map<String, Object>> getDoctorAnswer(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        List<Map<String, Object>> list=ymUserInfromMapper.getDoctorAnswer(userId);
        return list;
    }
    @Override
    public Integer insertUserInformation(YmUserInfrom ymUserInfrom) {
        return ymUserInfromMapper.insertUserInformation(ymUserInfrom);
    }



    @Override
    public List<Map<String, Object>> getExchangeAnswer(Long userId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        List<Map<String, Object>> list=ymUserInfromMapper.getExchangeAnswer(userId);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteOrder(Long userId, Long orderId) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        Assert.notNull(orderId,String.format(ErrorUtil.PARAMETER_NULL,"orderId"));
        Integer integer=ymUserInfromMapper.deleteOrder(userId,orderId);
        return integer;
    }


}
